import com.mongodb.BasicDBObject;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.model.UpdateOptions;
import org.bson.Document;

import java.io.*;

public class tonghuashunWanshan {
    public static void main(String[] args) {
        MongoCollection<Document> sourse = new MongoClient("192.168.1.174", 27017).getDatabase("KAMD").getCollection("KAMD_Date_2020");
        MongoCollection<Document> target = new MongoClient("192.168.1.174", 27017).getDatabase("KAMD").getCollection("关键审计事项20210617");
        File f = new File("E:\\年报\\2020年年报_TXT");
        FindIterable<Document> documents = sourse.find().sort(new Document("_id", 1));
        documents.noCursorTimeout(true);
        int count = 0;
        File[] f_all = f.listFiles();

        for (Document document : documents) {
            String code = document.getString("股票代码");
            String name = document.getString("名称");//项目名称
//            System.out.println(code);
//            System.out.println(name);

//            System.out.println(code);
//            System.out.println(name);
            for (File file :
                 f_all) {
                if(file.getName().startsWith(code)){
                    String name2 = txt2Name(file,name);
                    if(name2 != null && name2.contains("）")){
                        try{
                            name2 = name2.substring(name2.indexOf("）")+1).trim();
                        }catch (Exception e){

                        }
                    }
                    if(name2 != null && name2.contains("、")){
                        try{
                            name2 = name2.substring(name2.indexOf("、")+1).trim();
                        }catch (Exception e){

                        }
                    }
                    if(name2 != null && name2.contains(")")){
                        try{
                            name2 = name2.substring(name2.indexOf(")")+1).trim();
                        }catch (Exception e){

                        }
                    }
                    if(name2 != null && name2.contains(". ")){
                        try{
                            name2 = name2.substring(name2.indexOf(". ")+1).trim();
                        }catch (Exception e){

                        }
                    }
//                    System.out.println(name2);
                    document.append("项目名称2",name2);
//                    System.out.println(txt2Name(file,name));
                    break;
                }
            }
            document.append("关键审计事项内容",document.getString("关键审计事项内容").replace("\r","\r\n"));
            document.append("应对方法",document.getString("应对方法").replace("\r ","\r\n"));
            target.updateOne(new BasicDBObject("_id", document.get("_id")), new BasicDBObject("$set", document), (new UpdateOptions()).upsert(true));
            System.out.println(count++);
        }
    }
    public static String txt2Name(File file, String neme) {
//        StringBuilder result = new StringBuilder();
        boolean start = false;
        try {
            InputStream is = new FileInputStream(file);
            BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8"));//构造一个BufferedReader类来读取文件
            String s;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
//                result.append(System.lineSeparator() + s);
//                System.out.println(s);
                if(s.contains("关键审计事项")) start = true;
                if(start && s.contains(neme) ){
//                    System.out.println(s);
//                    break;
                    if(neme.contains("营业收入")){
                        if(s.contains("确认")){
                            return s;
                        }
//                        System.out.println(s);
                        break;
                    }else if (neme.contains("应收账款")){
                        if(s.contains("准备")){
                            return s;
                        }
                        if(s.contains("减值")){
                            return s;
                        }
                        if(s.contains("可收回性")){
                            return s;
                        }
                        if(s.contains("确认")){
                            return s;
                        }
                    } else {
                        return  s;
                    }

//                    break;
                }
            }
            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
//        String result2 = result.toString();
//        if (result2.contains("关键审计事项")) {
//            result2 = result2.substring(result2.indexOf("关键审计事项"));
//        }
//        return result2;
//        if(neme.contains("营业收入")) return "营业收入";
        return null;
    }
}
